﻿Public Class Kontainer
    Inherits System.Web.UI.Page

    Dim koneksidataakses7 As New KoneksiDataAkses
    Dim kueri As String

    Sub bersih()
        textbox1.Text = ""
        GridView1.SelectedIndex = -1
        dropdownlist2.DataBind()
        dropdownlist1.SelectedValue = 0
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GridView1.DataBind()
        tabel1.Visible = False
    End Sub



    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "btnCopy" Then
            bersih()
            textbox1.Text = GridView1.Rows(e.CommandArgument.ToString).Cells(1).Text
            dropdownlist2.Text = GridView1.Rows(e.CommandArgument.ToString).Cells(2).Text
            If GridView1.Rows(e.CommandArgument.ToString).Cells(3).Text = "Aktif" Then
                dropdownlist1.SelectedValue = 0
            ElseIf GridView1.Rows(e.CommandArgument.ToString).Cells(3).Text = "Pasif" Then
                dropdownlist1.SelectedValue = 1
            End If
            tabel1.Visible = True
            System.Web.HttpContext.Current.Session("status_aksiy") = True
        End If
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.SelectedIndexChanged
        dropdownlist1.DataBind()
        textbox1.Text = GridView1.SelectedRow.Cells(1).Text
        dropdownlist2.Text = GridView1.SelectedRow.Cells(2).Text
        If GridView1.SelectedRow.Cells(3).Text = "Aktif" Then
            dropdownlist1.SelectedValue = 0
        ElseIf GridView1.SelectedRow.Cells(3).Text = "Pasif" Then
            dropdownlist1.SelectedValue = 1
        End If
        System.Web.HttpContext.Current.Session("status_aksiy") = False
        tabel1.Visible = True
    End Sub

    Protected Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
        If Session("status_aksiy") = True Then
            koneksidataakses7.cekDuplikat("TB_MST_KONTAINER", "kode_kontainer", textbox1.Text)
            If koneksidataakses7.ds.Tables(0).Rows.Count = 0 Then
                kueri = "insert into TB_MST_KONTAINER (kode_kontainer,id_divisi,is_active,tgl_update ) values ('" + textbox1.Text + "',(select id_divisi from TB_MST_DIVISI where kode_divisi= '" + dropdownlist2.SelectedValue + "')," + dropdownlist1.SelectedValue + ",CURRENT_TIMESTAMP)"
                koneksidataakses7.EksekusiNonQuery(kueri)
                GridView1.DataBind()
            Else
                Response.Write("ada data yang sama")
            End If
        ElseIf Session("status_aksiy") = False Then
            kueri = " update TB_MST_KONTAINER set kode_kontainer='" + textbox1.Text + "', id_divisi=(select id_divisi from TB_MST_DIVISI where kode_divisi= '" + dropdownlist2.SelectedValue + "'),is_active=" + dropdownlist1.SelectedValue + ",tgl_update=current_timestamp where id_kontainer=" + GridView1.SelectedRow.Cells(0).Text + ""
            koneksidataakses7.EksekusiNonQuery(kueri)
            GridView1.DataBind()
        End If
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
        bersih()
        dropdownlist2.DataBind()
        tabel1.Visible = True
        System.Web.HttpContext.Current.Session("status_aksiy") = True
    End Sub

    Protected Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click
        bersih()
    End Sub

    Protected Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        bersih()
    End Sub

End Class